#include <TFT_eSPI.h>
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <SPI.h>

#ifndef TFT_DISPOFF
#define TFT_DISPOFF 0x28
#endif

#ifndef TFT_SLPIN
#define TFT_SLPIN   0x10
#endif

#define TIME 3000
#define POWER_ADC 27//ADC引脚

TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library
MPU6050 mpu6050(Wire);

void setup()
{
  Serial.begin(115200);
  Serial.println("Start");

  adcAttachPin(POWER_ADC);//将引脚连接到ADC
  adcStart(POWER_ADC);//在连接的引脚总线上开始ADC转换
  analogReadResolution(16);//设置aliogRead返回值的分辨率

  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);

  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
  tft.setTextSize(2);
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);

  if (TFT_BL > 0) { // TFT_BL has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
    pinMode(TFT_BL, OUTPUT); // Set backlight pin to output mode
    digitalWrite(TFT_BL, TFT_BACKLIGHT_ON); // Turn backlight on. TFT_BACKLIGHT_ON has been set in the TFT_eSPI library in the User Setup file TTGO_T_Display.h
  }

}

void loop()
{
  mpu6050.update();

  tft.setTextColor(TFT_RED, TFT_BLACK);
  tft.drawString("AngleX:", 0, 6, 1);
  tft.drawFloat(mpu6050.getAngleX(),2, 100, 6, 1);
  tft.drawString("*", 220, 6, 1);

  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  tft.drawString("AngleY:", 0, 36, 1);
  tft.drawFloat(mpu6050.getAngleY(),2, 100, 36, 1);
  tft.drawString("*", 220, 36, 1);

  tft.setTextColor(TFT_BLUE, TFT_BLACK);
  tft.drawString("AngleZ:", 0, 63, 1);
  tft.drawFloat(mpu6050.getAngleZ(),2, 100, 63, 1);
  tft.drawString("*", 220, 63, 1);

  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  float power_adc = 2*((analogRead(POWER_ADC)*3.3)/65535)+0.4;
  tft.drawString("V:", 0, 93, 1);
  tft.drawFloat(power_adc,2, 100, 93, 1);
  tft.drawString("V", 220, 93, 1);

  delay(50);
  tft.fillScreen(TFT_BLACK);
}